package com.hgxd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgxd.annotation.OperationLogAnnotation;
import com.hgxd.entity.pojo.SysRole;
import com.hgxd.entity.vo.ReqUserRoleVo;
import com.hgxd.service.SysRoleService;
import com.hgxd.service.SysUserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;
import java.util.List;

import com.hgxd.result.Result;


import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author fsd
 * @since 2023-10-18
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/api/sysRole")
@CrossOrigin
public class SysRoleController {
    @Resource(name = "sysRoleServiceImpl")
    private SysRoleService sysRoleService;

    @Resource(name = "sysUserRoleServiceImpl")
    private SysUserRoleService sysUserRoleService;

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增角色", notes = "角色对象，需传入创建者的id")
    @ApiImplicitParam(name = "sysRole", value = "sysRole对象")
    @OperationLogAnnotation(operModule = "角色管理-新增",operType = "新增",operContent = "新增角色")
    public Result<Boolean> insert(@RequestBody SysRole sysRole) {
        return Result.ok(sysRoleService.save(sysRole));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改角色", notes = "传入要修改的对象，未更改部分无需传入")
    @OperationLogAnnotation(operModule = "角色管理-修改",operType = "修改",operContent = "修改角色")
    public Result<Boolean> update(@RequestBody SysRole sysRole) {
        return Result.ok(sysRoleService.updateById(sysRole));
    }

    /**
     * 根据roleId删除
     */
    @PostMapping("/delete/{roleId}")
    @ApiOperation(value = "根据roleId删除", notes = "角色id(roleId)")
    @ApiImplicitParam(name = "roleId", value = "角色id")
    @OperationLogAnnotation(operModule = "角色管理-根据id删除",operType = "删除",operContent = "根据roleId删除角色")
    public Result<Boolean> delete(@PathVariable("roleId") Integer roleId) {
        return Result.ok(sysRoleService.removeById(roleId));
    }

    /**
     * 批量删除
     */
    @PostMapping("/del/batch")
    @ApiOperation(value = "批量删除", notes = "传入id数组")
    @ApiImplicitParam(name = "ids", value = "id数组")
    @OperationLogAnnotation(operModule = "角色管理-批量删除",operType = "删除",operContent = "根据ids批量删除")
    public Result<Boolean> deleteBatch(@RequestBody List<Integer> ids) {
        return Result.ok(sysRoleService.removeByIds(ids));
    }

    /**
     * 查询全部
     */
    @GetMapping("/getAll")
    @ApiOperation(value = "查询全部", notes = "查询全部(无需传入参数)")
    @OperationLogAnnotation(operModule = "角色管理-查询全部",operType = "查询",operContent = "查询全部角色")
    public Result<List<SysRole>> findAll() {
        return Result.ok(sysRoleService.list());
    }

    /**
     * 根据roleId查询
     */
    @GetMapping("getById/{roleId}")
    @ApiOperation(value = "根据roleId查询", notes = "角色id")
    @ApiImplicitParam(name = "roleId", value = "角色id")
    @OperationLogAnnotation(operModule = "角色管理-根据id查询",operType = "查询",operContent = "根据roleId查询角色")
    public Result<SysRole> findOne(@PathVariable Integer roleId) {
        return Result.ok(sysRoleService.getById(roleId));
    }

    /**
     * 分页模糊查询
     */
    @GetMapping("/get/page")
    @ApiOperation(value = "分页模糊查询", notes = "传入必备pageNum和pageSize")
    @OperationLogAnnotation(operModule = "角色管理-分页模糊查询",operType = "查询",operContent = "分页模糊查询")
    public Result<Page<SysRole>> findPage(@ApiParam(name = "pageNum",value = "页码",required = true) @RequestParam Integer pageNum,
                                          @ApiParam(name = "pageSize",value = "大小",required = true) @RequestParam Integer pageSize,
                                          @ApiParam(name = "roleName",value = "角色名称") @RequestParam(required = false,defaultValue = "") String roleName) {
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        // 倒序查询
        queryWrapper.orderByDesc(SysRole::getRoleId);
        queryWrapper.eq(!"".equals(roleName),SysRole::getRoleName,roleName);
        return Result.ok(sysRoleService.page(new Page<>(pageNum, pageSize),queryWrapper));
    }

    @PostMapping("/getRoleByUserId/{userId}")
    @ApiOperation(value = "根据userId查询roleId", notes = "传入必备userId")
    @OperationLogAnnotation(operModule = "角色管理-根据userId查询roleId",operType = "查询",operContent = "根据userId查询roleId")
    public Result<List<Long>> getRoleByUserId(@PathVariable("userId") Long userId){
        return Result.ok(sysRoleService.getRoleByUserId(userId));
    }

    @ApiOperation(value = "根据userId修改roleId", notes = "传入必备userId和对应修改的数组")
    @OperationLogAnnotation(operModule = "角色管理-根据userId修改roleId",operType = "修改",operContent = "根据userId修改roleId")
    @PostMapping("/updateRoleByUserId")
    public Result<Boolean> updateRoleByUserId(@RequestBody ReqUserRoleVo reqUserRoleVo){
        return Result.ok(sysUserRoleService.updateRoleByUserId(reqUserRoleVo));
    }

}

